/*
 * Copyright (C) 2011 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.provider.storage.db;

import android.location.Location;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import ch.bfh.aeneas.exchange.intent.AeneasIntent;
import ch.bfh.aeneas.exchange.data.TrackStorageBinder;
import ch.bfh.aeneas.exchange.data.Node;
import ch.bfh.aeneas.exchange.data.Route;
import ch.bfh.aeneas.exchange.data.TrackStorage;
import ch.bfh.aeneas.exchange.data.NoItemFoundException;
import ch.bfh.aeneas.exchange.storage.StorageGeoProvider;
import java.util.List;

/**
 *
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class DatabaseService extends Service implements TrackStorage {


    public class DatabaseBinder extends Binder implements TrackStorageBinder {

        public DatabaseService getService() {
            return DatabaseService.this;
        }
    }
    private final DatabaseBinder mBinder = new DatabaseBinder();
    private Database mDb;
    //private SQLiteDatabase mDb2;
    private StorageGeoProvider mStorageProvider;
    
    private final String LOG_TAG = "DatabaseService";
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate() {
        super.onCreate();
        
        Log.i(LOG_TAG, "DB-Service started!");
        
        mDb = Database.getInstance(new DBOpenHelper(this));
        
        mStorageProvider = new StorageGeoProvider(mDb);
        
        IntentFilter infi = new IntentFilter(AeneasIntent.ACTION_REQUEST);
        infi.addCategory(AeneasIntent.CATEGORY_DATA);
        getApplicationContext().registerReceiver(mStorageProvider, infi);
        
        infi = new IntentFilter(AeneasIntent.ACTION_REQUEST);
        infi.addCategory(AeneasIntent.CATEGORY_ROUTE);
        getApplicationContext().registerReceiver(mStorageProvider, infi);
        
        infi = new IntentFilter(AeneasIntent.ACTION_REQUEST);
        infi.addCategory(AeneasIntent.CATEGORY_NODE);
        getApplicationContext().registerReceiver(mStorageProvider, infi);
        
        infi = new IntentFilter(AeneasIntent.ACTION_REQUEST);
        infi.addCategory(AeneasIntent.CATEGORY_TRACKSTORAGE);
        getApplicationContext().registerReceiver(mStorageProvider, infi);
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        getApplicationContext().unregisterReceiver(mStorageProvider);
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    
    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }
    
    public Route getRouteById(long id) throws NoItemFoundException {
        return mDb.getRouteById(id);
    }
    
    public Route getRouteByName(String name) throws NoItemFoundException {
        return mDb.getRouteByName(name);
    }
    
    public List<Route> getRoutes() throws NoItemFoundException {
        return mDb.getRoutes();
    }
    
    public List<Route> getRoutesByNode(Node node) throws NoItemFoundException {
        return mDb.getRoutesByNode(node);
    }
    
    public List<Route> getRoutesNear(Location loc, int distance) throws NoItemFoundException {
        return mDb.getRoutesNear(loc, distance);
    }

    public List<Route> getRoutesNear(double latitude, double longitude, int distance) throws NoItemFoundException {
        return mDb.getRoutesNear(latitude, longitude, distance);
    }

    public Node addPOI(String name, double latitude, double longitude, double altitude, float accuracy) {
        return mDb.addPOI(name, latitude, longitude, altitude, accuracy);
    }

    public Node addNode(String name, Location loc) {
        return mDb.addNode(name, loc);
    }

    public Node addNode(String name, double latitude, double longitude, double altitude, float accuracy) {
        return mDb.addNode(name, latitude, longitude, altitude, accuracy);
    }

    public boolean convertToPOI(Node node) {
        return mDb.convertToPOI(node);
    }

    public Node getNodeByName(String name) throws NoItemFoundException {
        return mDb.getNodeByName(name);
    }
    
    public Node getNodeById(long id) throws NoItemFoundException {
        return mDb.getNodeById(id);
    }
    
    public Node getNodeByLocation(Location loc) throws NoItemFoundException {
        return mDb.getNodeByLocation(loc);
    }
    
    public Node getNodeByLocation(double latitude, double longitude, double altitude) throws NoItemFoundException {
        return mDb.getNodeByLocation(latitude, longitude, altitude);
    }

    public List<Node> getNodesByRoute(Route r) {
        return mDb.getNodesByRoute(r);
    }
    
    public List<Node> getPois() throws NoItemFoundException {
        return mDb.getPois();
    }

    public Route addRoute(String name) {
        return mDb.addRoute(name);
    }

    public Node addPOI(String name, Location loc) {
        return mDb.addPOI(name, loc);
    }

    public boolean addRouteNode(Route route, Node node) {
        return mDb.addRouteNode(route, node);
    }

    public List<Node> findNodesNear(double latitude, double longitude, int distance) {
        return mDb.findNodesNear(latitude, longitude, distance);
    }
    
    public List<Node> findNodesNear(Node node, int distance) throws NoItemFoundException {
        return mDb.findNodesNear(node, distance);
    }
}
